package com.hoperun.checkcloth.activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.hoperun.checkcloth.R;
import com.hoperun.checkcloth.base.MIP_BaseActivity;
import com.hoperun.checkcloth.entity.JyZhiZaoModel;
import com.hoperun.checkcloth.net.IServiceImpl;
import com.hoperun.checkcloth.net.RequestServiceImpl;
import com.hoperun.checkcloth.ui.dialog.SimpleDialog;
import com.hoperun.checkcloth.util.MIP_SystemUtils;
import com.hoperun.checkcloth.util.ParseUtils;
import com.hoperun.checkcloth.util.SharedPreUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ChangziQueryActivity extends MIP_BaseActivity implements View.OnClickListener {
    RecyclerView recylistview;
    TextView input_dateTv,input_dateTv2;
    LinearLayout input_dateLl,input_dateLl2;
    EditText editbizno,editchehao1,editchehao2;
    Spinner spinnerchejian;
    QuickAdapter quickAdapter;
    List<JyZhiZaoModel> jyzhizaolist = new ArrayList<JyZhiZaoModel>();

    ArrayAdapter spinnerAdapter;
    ArrayList<String> chejianlist = new ArrayList<String>();
    JSONArray chearray=null;
    //日期
    String mJobDate1,mJobDate2;
    int choosetime=0;//1开始 2结束
    private SimpleDateFormat dateFormat_CHINA = new SimpleDateFormat("yyyy年MM月dd日");
    private SimpleDateFormat dateFormat_EN = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_zhizao_query);
        recylistview=(RecyclerView)this.findViewById(R.id.recylistview);
        input_dateTv=(TextView)this.findViewById(R.id.input_dateTv);
        input_dateLl=(LinearLayout)this.findViewById(R.id.input_dateLl);
        input_dateTv2=(TextView)this.findViewById(R.id.input_dateTv2);
        input_dateLl2=(LinearLayout)this.findViewById(R.id.input_dateL2);

        editbizno=(EditText)this.findViewById(R.id.editbizno);
        editchehao1=(EditText)this.findViewById(R.id.editchehao1);
        editchehao2=(EditText)this.findViewById(R.id.editchehao2);

        spinnerchejian=(Spinner)this.findViewById(R.id.spinnerchejian);


        this.findViewById(R.id.queryBtn).setOnClickListener(this);
        this.findViewById(R.id.input_dateLl).setOnClickListener(this);
        this.findViewById(R.id.input_dateL2).setOnClickListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recylistview.setLayoutManager(layoutManager);
        //给RecyclerView设置适配器
        quickAdapter = new QuickAdapter();
        recylistview.setAdapter(quickAdapter);
        recylistview.setHasFixedSize(true);
        recylistview.setNestedScrollingEnabled(false);
        RequestServiceImpl.jy_getsysdate(mMIPHandler, IServiceImpl.JY_GETTIME);

        //工种
        chejianlist.add("请选择");
        spinnerAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, chejianlist);
        spinnerAdapter.setDropDownViewResource(R.layout.spinner_drop_item);
        spinnerchejian.setAdapter(spinnerAdapter);
        RequestServiceImpl.zz_getchejian(mMIPHandler, IServiceImpl.ZZ_SPIINER);

    }
    public class QuickAdapter extends BaseQuickAdapter<JyZhiZaoModel, BaseViewHolder> {
        public QuickAdapter() {
            super(R.layout.item_zhizao_query, jyzhizaolist);
        }

        @Override
        protected BaseViewHolder createBaseViewHolder(View view) {
            return super.createBaseViewHolder(view);
        }

        @Override
        protected void convert(BaseViewHolder holder, final JyZhiZaoModel item) {
            TextView item_time = holder.getView(R.id.item_time);
            TextView item_jyg = holder.getView(R.id.item_jyg);
            TextView item_pibuming = holder.getView(R.id.item_pibuming);
            TextView item_pizhong = holder.getView(R.id.item_pizhong);
            TextView item_chehao = holder.getView(R.id.item_chehao);
            TextView item_sanlianhao = holder.getView(R.id.item_sanlianhao);
            TextView  item_gonghao= holder.getView(R.id.item_gonghao);
            TextView  item_geshu= holder.getView(R.id.item_geshu);
            TextView  item_changzi= holder.getView(R.id.item_changzi);
            TextView  item_beizhu= holder.getView(R.id.item_beizhu);
            TextView  item_zhuangtai= holder.getView(R.id.item_zhuangtai);
            TextView  item_querengren= holder.getView(R.id.item_querengren);

            item_time.setText(item.getLISTDATE() == null ? "" : item.getLISTDATE());
            item_jyg.setText(item.getJYGBH() == null ? "" : item.getJYGBH());
            item_pibuming.setText(item.getCLOTHNO() == null ? "" : item.getCLOTHNO());
            item_pizhong.setText(item.getWEIGHT()== null ? "" : item.getWEIGHT());
            item_chehao.setText(item.getMACHINENO()== null ? "" : item.getMACHINENO());
            item_sanlianhao.setText(item.getCLOTHTKTNO() == null ? "" : item.getCLOTHTKTNO());
            item_gonghao.setText(item.getEMPNO()== null ? "" : item.getEMPNO());
            item_geshu.setText(item.getCELLNO()== null ? "" : item.getCELLNO());
            item_changzi.setText(item.getLONGFAULT()== null ? "" : item.getLONGFAULT());
            item_beizhu.setText(item.getREMARK()==null?"":item.getREMARK());
            item_zhuangtai.setText(item.getZT()== null ? "" : item.getZT());
            item_querengren.setText(item.getSUBNAME()== null ? "" : item.getSUBNAME());
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.input_dateLl:
                choosetime=1;
                setDate();
                break;
            case R.id.input_dateL2:
                choosetime=2;
                setDate();
                break;

            case R.id.queryBtn:
            {
                if(chearray!=null){
                    String cheid="";
                    for(int i=0;i<chearray.length();i++){
                        try {
                            JSONObject object= (JSONObject) chearray.get(i);
                            if(object.getString("BMMC").equals(spinnerchejian.getSelectedItem().toString())){
                                cheid=object.getString("BMXXID");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                    querylist(cheid,mJobDate1,mJobDate2,editbizno.getEditableText().toString().trim(),editchehao1.getEditableText().toString().trim(),editchehao2.getEditableText().toString().trim());
                }else{
                    showSimpleDialog("车间不能为空");
                }

            }
            break;
        }
    }

    public void querylist(String WORKSHOP, String LISTDATE_FROM, String LISTDATE_TO, String CLOTHTKTNO, String MACHINENO_FROM,String MACHINENO_TO) {//根据条件查询
        showWait();
        RequestServiceImpl.zz_query(mMIPHandler, IServiceImpl.ZZ_QUERYLIST, WORKSHOP, LISTDATE_FROM, LISTDATE_TO, CLOTHTKTNO, MACHINENO_FROM,MACHINENO_TO);
    }


    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
        stopWait();
        String msg = "";
        String opt = "";
        switch (requestType) {
            case IServiceImpl.ZZ_QUERYLIST://根据条件查询
                try {
                    JSONObject jsonObj = new JSONObject((String) objBody);
                    msg = jsonObj.optString("MSG_INFO");
                    opt = jsonObj.optString("OPT_FLAG");
                    if ("SUCCESS".equals(opt)) {
                        JSONArray dataarray = jsonObj.optJSONArray("DATA");
                        if (dataarray != null&&dataarray.length()>0) {
                            jyzhizaolist.clear();
                            for (int i = 0; i < dataarray.length(); i++) {
                                JyZhiZaoModel zhizaoModel = ParseUtils.parse(dataarray.getJSONObject(i).toString(), JyZhiZaoModel.class);
                                jyzhizaolist.add(zhizaoModel);
                            }

                        } else {
                            jyzhizaolist.clear();
                        }
                        quickAdapter.notifyDataSetChanged();
                        checkListview();
                    } else {
                        if (TextUtils.isEmpty(msg)) {
                            showSimpleDialog("查询出错");
                        } else {
                            showSimpleDialog(msg);
                        }

                    }

                }catch (Exception e){
                }
                break;

            case IServiceImpl.JY_GETTIME://获取时间
                try {
                    if (!TextUtils.isEmpty((String) objBody)) {
                        mJobDate1=dateFormat_EN.format(dateFormat_EN.parse(objBody.toString()));
                        input_dateTv.setText(dateFormat_CHINA.format(dateFormat_EN.parse(objBody.toString())));

                        mJobDate2=dateFormat_EN.format(dateFormat_EN.parse(objBody.toString()));
                        input_dateTv2.setText(dateFormat_CHINA.format(dateFormat_EN.parse(objBody.toString())));
                    }

                }catch (Exception e){
                }
                break;

            case IServiceImpl.ZZ_SPIINER:
                try {
                    chearray = new JSONArray((String) objBody);
                    if(chearray!=null){
                        for(int i=0;i<chearray.length();i++){
                            JSONObject object= (JSONObject) chearray.get(i);
                            chejianlist.add(object.getString("BMMC"));
                        }
                        spinnerAdapter.notifyDataSetChanged();
                    }

                }catch (Exception e){
                }
                break;


        }
    }

    private void showSimpleDialog(String message) {
        SimpleDialog.Companion.newInstance(message).show(getSupportFragmentManager(), "DialogTip");
    }
    /**
     * 检查listview是否为空
     */
    private void checkListview() {
        if (jyzhizaolist.size() > 0) {
            recylistview.setVisibility(View.VISIBLE);
        } else {
            recylistview.setVisibility(View.GONE);
        }
    }

    /**
     * 设置日期
     */
    private void setDate() {
        String[] strNow;
        if(choosetime==1){
            strNow= mJobDate1.toString().split("-");
        }else{
            strNow= mJobDate2.toString().split("-");
        }
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, mOnDateSetListenernew, Integer.parseInt(strNow[0]), Integer.parseInt(strNow[1])-1, Integer.parseInt(strNow[2]));
        datePickerDialog.show();
    }

    /**
     * 日期选择监听器
     */
    DatePickerDialog.OnDateSetListener mOnDateSetListenernew = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            String date = year + "年" + String.format("%02d", month + 1) + "月" + String.format("%02d", dayOfMonth)+"日";
            if(choosetime==1){
                input_dateTv.setText(date);
                mJobDate1 = year + "-" + String.format("%02d", month + 1) + "-" + String.format("%02d", dayOfMonth);
            }else{
                input_dateTv2.setText(date);
                mJobDate2 = year + "-" + String.format("%02d", month + 1) + "-" + String.format("%02d", dayOfMonth);

            }
        }
    };


}
